package com.hzw.saas.web.admin.job.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hzw.saas.common.quartz.model.ScheduleJob;
import com.hzw.saas.common.util.StringUtils;
import com.hzw.saas.common.util.UnitConvertUtils;
import com.hzw.saas.common.util.enums.TimeUnitEnum;
import com.hzw.saas.service.storage.service.ResourceMigrateService;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author EZ09
 * @since 07/12/2021
 */
@Service("fileMigrationTask")
@RequiredArgsConstructor
@Slf4j
public class FileMigrationTask {

    private static final int DEFAULT_THREAD_COUNT = 5;
    private static final String DEFAULT_OFFSET_TIME = "14d";

    private final ResourceMigrateService migrateService;

    /**
     * 冷数据迁移
     *
     * @param scheduleJob 迁移任务参数
     */
    @SneakyThrows
    public void execute(ScheduleJob scheduleJob) {
        log.info("资源文件冷数据迁移开始...");
        // 执行线程数
        int threadCount = this.getThreadCount(scheduleJob.getParams());
        // 待查询的工作空间
        List<String> spaceIds = this.getSpaceIds(scheduleJob.getParams());
        // 查询截止日期
        Date beforeDate = this.getBeforeDate(scheduleJob.getParams());
        log.info("冷数据迁移参数: {threadCount: {},spaces: {}, beforeDate: {}}",
            threadCount, JSONUtil.toJsonStr(spaceIds), DateUtil.formatDateTime(beforeDate));

        migrateService.migrateFiles2Cloud(spaceIds, null, beforeDate, threadCount);
    }

    private int getThreadCount(String params) {
        try {
            JSONObject paramJson = JSONUtil.parseObj(params);
            return paramJson.getInt("threadCount", DEFAULT_THREAD_COUNT);
        } catch (Exception e) {
            return DEFAULT_THREAD_COUNT;
        }
    }

    private List<String> getSpaceIds(String params) {
        try {
            JSONObject paramJson = JSONUtil.parseObj(params);
            String spaceIds = paramJson.getStr("spaceIds", "");
            List<String> spaceIdList = ListUtil.list(true, spaceIds.split("[,;]"));
            spaceIdList = StringUtils.trimAndDistinct(spaceIdList);
            spaceIdList.removeIf(StrUtil::isBlank);
            return spaceIdList;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    private Date getBeforeDate(String params) {
        Date beforeDate;
        Date now = new Date();
        try {
            JSONObject paramJson = JSONUtil.parseObj(params);
            String offsetTime = paramJson.getStr("offsetTime", DEFAULT_OFFSET_TIME);

            if (UnitConvertUtils.isDateTime(offsetTime)) {
                beforeDate = UnitConvertUtils.offsetDatetime(now, offsetTime);
            } else {
                beforeDate = UnitConvertUtils.offsetDatetime(now, offsetTime.concat(TimeUnitEnum.DAY.text()));
            }
        } catch (Exception e) {
            beforeDate = UnitConvertUtils.offsetDatetime(now, DEFAULT_OFFSET_TIME);
        }
        return beforeDate;
    }


}
